# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(plyr)
library(gridExtra)
library(scales)
library(egg)
library(VennDiagram)
library(optparse)
library(tidyverse)
library(magrittr)
library(ggvenn)

createWhenNoExist <- function(f){
    ! dir.exists(f) && dir.create(f)
}

option_list <- list(
make_option("--i", default = "AllMet.csv", type = "character", help = "metabolite data file"),
make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
make_option("--config", default = "config.csv", type = "character", help = "config file")
)
opt <- parse_args(OptionParser(option_list = option_list))

configData <- read.csv(opt$config, header = F, stringsAsFactors = F) %>%
    set_colnames(c("arg", "value")) %>%
    column_to_rownames("arg")

svmTop <- configData["svmTop", "value"] %>%
as.numeric()
rfTop <- configData["rfTop", "value"] %>%
as.numeric()

sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
select(c("SampleID", "ClassNote"))

parent <- paste0("./")
createWhenNoExist(parent)
rfFileName <- str_c("01/RF_Top", rfTop, "_Imp_Rank.csv")

if(!file.exists(rfFileName)){
    quit(status = 0)
}

data <- read_csv(rfFileName)
rfData <- data %>%
.$Metabolite
svmFileName <- str_c("02/SVM_Top", svmTop, "_Imp_Rank.csv")
svmData <- read_csv(svmFileName) %>%
.$Metabolite
borutaFileName <- str_c("03/Decision_Info.csv")
borutaData <- read_csv(borutaFileName) %>%
    rename(Metabolite = X1) %>%
    filter(decision == "Confirmed") %>%
    .$Metabolite
pdfFileName <- paste0(parent, "/Venn_Plot.pdf")
pdf(pdfFileName, width = 8, height = 8)

data <- list(`Random Forest` = rfData,
             `SVM` = svmData,
             `Boruta`=borutaData
)
ggvenn(data = data, fill_color = c("#4040FF", "#FF4040", "#00FF00"), stroke_size = 1.5, set_name_size = 12, show_percentage = F,
       fill_alpha = .6, text_size = 8)
dev.off()





